package com.parlons.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.parlons.R;
import com.parlons.SmackServices;

/**
 * Created by wangyifan on 15/12/21.
 */
public class SignUpActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        Button signUpButton = (Button) findViewById(R.id.sign_up_button);
        TextView signUpToLoginText = (TextView) findViewById(R.id.sign_up_to_login);
        signUpToLoginText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SignUpActivity.this, LogInActivity.class);
                startActivity(i);
                finish();
            }
        });
        signUpButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                System.out.println("log in!");
                EditText usernameInput = (EditText) findViewById(R.id.input_sign_up_username);
                final String username = usernameInput.getText().toString().trim();
                EditText passwordInput = (EditText) findViewById(R.id.input_sign_up_password);
                final String password = passwordInput.getText().toString().trim();
                if (SmackServices.register(username, password) == 1) {
                    SmackServices.login(username, password);
                    System.out.println("successfully logged in!");
                    System.out.println(SmackServices.getConnection().isAuthenticated());
                    Toast toast = Toast.makeText(getApplicationContext(),
                            "注册成功!", Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();

                    //save the default user
                    SharedPreferences settings = getSharedPreferences("parlons", Activity.MODE_PRIVATE);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("default_user", username);
                    editor.putString("password", password);
                    editor.commit();

                    Intent i = new Intent(SignUpActivity.this, MainActivity.class);
                    startActivity(i);
                    finish();
                } else {
                    Toast toast = Toast.makeText(getApplicationContext(),
                            "注册失败!", Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            }
        });
    }
}
